package com.woody.aggregate.app.web.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.woody.aggregate.application.service.contract.ShopPerCustomerService;
import com.woody.aggregate.contract.dto.ShopPerCustomerDTO;
import com.woody.aggregate.contract.vo.ContractVO;
import com.woody.aggregate.contract.vo.ShopPerCustomerVO;
import com.woody.aggregate.infrastructure.contract.entity.ShopPerCustomerEntity;
import com.woody.base.exception.BaseException;
import com.woody.base.model.Response;
import com.woody.common.web.util.JacksonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author luke.feng
 * @Description
 * @create 2025/4/1 17:22
 */
@RestController
@RequestMapping("/shop/per")
@AllArgsConstructor
@Api(tags = "店铺客单价接口")
@Slf4j
public class ShopPerCustomerController {

    private final ShopPerCustomerService shopPerCustomerService;

    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "新增或编辑", notes = "新增或编辑")
    public Response saveOrUpdate(@RequestBody ShopPerCustomerDTO shopPerCustomerDTO){
        if (Objects.isNull(shopPerCustomerDTO.getCustomerUnitPrice()) || Objects.isNull(shopPerCustomerDTO.getShopId())) {
            throw new BaseException("参数异常");
        }
        if (shopPerCustomerDTO.getCustomerUnitPrice().compareTo(BigDecimal.ZERO) <= 0) {
            throw new BaseException("客单价不能为0");
        }
        if (shopPerCustomerDTO.getCustomerUnitPrice().compareTo(new BigDecimal("100000000")) > 0) {
            throw new BaseException("客单价最大为1亿");
        }
        ShopPerCustomerEntity shopPerCustomerEntity = shopPerCustomerService.getBaseMapper().selectOne(new LambdaQueryWrapper<ShopPerCustomerEntity>().eq(ShopPerCustomerEntity::getShopId, shopPerCustomerDTO.getShopId()));
        if (Objects.nonNull(shopPerCustomerEntity)) {
            shopPerCustomerEntity.setCustomerUnitPrice(shopPerCustomerDTO.getCustomerUnitPrice());
            shopPerCustomerService.updateById(shopPerCustomerEntity);
            return Response.success();
        }
        shopPerCustomerService.save(JacksonUtil.reGroup(shopPerCustomerDTO, ShopPerCustomerEntity.class));
        return Response.success();
    }

    @GetMapping("/get")
    @ApiOperation(value = "查询", notes = "查询")
    public Response<ShopPerCustomerVO> get(@RequestParam("shopId") Long shopId){
        ShopPerCustomerEntity shopPerCustomerEntity = shopPerCustomerService.getBaseMapper().selectOne(new LambdaQueryWrapper<ShopPerCustomerEntity>().eq(ShopPerCustomerEntity::getShopId, shopId));
        if (Objects.isNull(shopPerCustomerEntity)) {
            return Response.success();
        }
        return Response.success(JacksonUtil.reGroup(shopPerCustomerEntity, ShopPerCustomerVO.class));
    }
}
